// pages/search/search.js
import Dialog from '@vant/weapp/dialog/dialog';

const historyStorageKey = "historyStorageKey"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "",
        keyword: "",
        historyList: [],
        productionList: [],
        noData: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // return
        var historyList = wx.getStorageSync(historyStorageKey)
        if (!historyList) {
            historyList = ["123", "456"]
        }
        this.setData({
            historyList: historyList
        })
        console.log(historyList);
    },

    onChange(e) {
        console.log(e);
        this.setData({
            value: e.detail
        })
    },

    onSearch(e) {
        console.log("onSearch");
        let keyword = this.data.value
        var history = this.data.historyList || []
        history.unshift(keyword)
        history = [...new Set(history)]
        this.setData({
            keyword: keyword,
            historyList: history
        })
    },

    onClear() {
        console.log("onClear");
        this.setData({
            value: "",
            keyword: "",
            productionList: [],
            noData: false
        })
    },

    onClearHistory() {
        console.log("onClearHistory");
        Dialog.confirm({
            title: "是否真的清空历史？"
        }).then(() => {
            console.log("confirm");
        }).catch(() => {
            console.log("cancel");
        })
    },

    onHistoryTagClick(e) {
        console.log(e.currentTarget.dataset.item);
        let keyword = e.currentTarget.dataset.item
        this.setData({
            value: keyword,
            keyword: keyword
        })
        this.onSearch(null)
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})